package han.cirno.corrupt.widgets

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ToggleButton
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import han.cirno.corrupt.R
import han.cirno.corrupt.databinding.ListWeekSelectBinding

class WeekSelectAdapter(val context: Context, private val initialArray:IntArray,private val enable:Boolean):Adapter<WeekSelectAdapter.Holder>() {
    val views=ArrayList<ToggleButton>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
        val view = LayoutInflater.from(context).inflate(R.layout.list_week_select, parent, false)
        views.add(view as ToggleButton)
        return Holder(view)
    }

    override fun onBindViewHolder(holder: Holder, position: Int) {
        val s = "${position + 1}"
        holder.binding.root.run { textOff=s;textOn=s;text=s; }
        if (initialArray.contains(position+1))
            holder.binding.root.isChecked=true
        holder.binding.root.isEnabled=enable
    }

    override fun getItemCount(): Int {
        return 20
    }

    fun setChecked(index:Int,checked:Boolean){
        views[index].isChecked=checked
    }

    fun getChecked(index: Int):Boolean{
        return views[index].isChecked
    }

    inner class Holder(val view: View):ViewHolder(view){
        val binding: ListWeekSelectBinding= ListWeekSelectBinding.bind(view)
    }
}